﻿Imports VN.INS.BL.BC
Imports System.Data
Imports Microsoft.Office.Interop
Imports System.IO

Partial Class Devida_Lista_Consolidado
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            cargar_grilla_prin(Session("PPER").ToString)
        End If
    End Sub

    'carga grilla principal
    Public Sub cargar_grilla_prin(ByVal PPER As String)
        Dim ObjconsolidadoBC As consolidadoBC
        Dim ObjData As DataTable
        ObjconsolidadoBC = New consolidadoBC
        ObjData = ObjconsolidadoBC.cargar_grilla_prin(PPER, Session("Año"))
        gv_listaconsolidado.DataSource = ObjData
        gv_listaconsolidado.DataBind()
    End Sub

    Protected Sub gv_listaconsolidado_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_listaconsolidado.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' ASIGNA EVENTOS
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gv_listaconsolidado, "Select$" + e.Row.RowIndex.ToString)
        End If
    End Sub

    Protected Sub gv_listaconsolidado_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_listaconsolidado.SelectedIndexChanged
        'captura de valores de fila seleccionada
        Dim htxtcod As HiddenField = CType(gv_listaconsolidado.SelectedRow.FindControl("codig"), HiddenField)
        Dim strNombre As String = gv_listaconsolidado.SelectedRow.Cells(3).Text.ToString
        Dim strfecha As String = gv_listaconsolidado.SelectedRow.Cells(4).Text.ToString
        Try
            ViewState("PERIOD") = strNombre
            ViewState("Fecha") = strfecha
            If Not htxtcod Is Nothing Then
                ViewState("codig") = htxtcod.Value.ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Protected Sub btn_modificar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_modificar.Click
        'redireccionamos registro seleccionado a detalle edicion
        If gv_listaconsolidado.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Response.Redirect("consolidado_avance_Mensual.aspx?Param=" + ViewState("codig").ToString & "&PERIOD=" & ViewState("PERIOD") & "&Fecha=" & ViewState("Fecha") & "&Nuevo=0")
        End If
    End Sub

    Protected Sub btn_nuevo_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_nuevo.Click
        Dim m As Integer = Convert.ToInt32(Now.Month) - 1
        Dim ano As Integer = Convert.ToInt32(Now.Year)
        Dim fech As String = m.ToString + "-" + ano.ToString
        Dim i As Integer = gv_listaconsolidado.Rows.Count - 1
        'redireccionamos registro seleccionado a detalle nuevo
        If i >= 0 Then
            If fech = gv_listaconsolidado.Rows(i).Cells(3).Text() Then
                alerta.Mostrar_Alerta("Aviso", "Ya se ingreso Informe Consolidado para el mes de " + fech + "")
            Else
                Response.Redirect("consolidado_avance_Mensual.aspx?Param=&Fecha=" & "&Nuevo=1")

            End If
        Else
            Response.Redirect("consolidado_avance_Mensual.aspx?Param=&Fecha=" & "&Nuevo=1")

        End If
    End Sub

    'salir
    Protected Sub ibn_salir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibn_salir.Click
        Response.Redirect("Inicio.aspx")
    End Sub

    Protected Sub btn_eliminar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_eliminar.Click
        'elimina registro seleccionado
        If gv_listaconsolidado.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Dim objConsolidado As consolidadoBC
            objConsolidado = New consolidadoBC
            objConsolidado.EliminarInforme(ViewState("codig").ToString)
            cargar_grilla_prin(Session("PPER").ToString)
            alerta.Mostrar_Alerta("Alerta", "Informe eliminado correctamente")
        End If
    End Sub
End Class